package com.uniledger.exchange.contact;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.uniledger.exchange.R;
import com.uniledger.exchange.glide.GlideApp;
import com.yqritc.recyclerviewflexibledivider.FlexibleDividerDecoration;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.yokeyword.indexablerv.IndexableAdapter;

public class ContactAdapter extends IndexableAdapter<UniContact> implements FlexibleDividerDecoration.VisibilityProvider {
    static final int TYPE_TITLE = Integer.MAX_VALUE - 1;
    private LayoutInflater inflater;
    private Context context;

    public ContactAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    /**
     * 标题栏
     *
     * @param parent
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateTitleViewHolder(ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_uni_index_contact, parent, false);
        return new IndexViewHolder(view);
    }

    /**
     * 内容栏
     *
     * @param parent
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_uni_contact, parent, false);
        return new ContactViewHolder(view);
    }

    @Override
    public void onBindTitleViewHolder(RecyclerView.ViewHolder holder, String indexTitle) {
        IndexViewHolder indexViewHolder = (IndexViewHolder) holder;
        indexViewHolder.indexTitle.setText(indexTitle);
    }

    @Override
    public void onBindContentViewHolder(RecyclerView.ViewHolder holder, UniContact entity) {
        ContactViewHolder contactViewHolder = (ContactViewHolder) holder;
        contactViewHolder.name.setText(entity.nick);
        contactViewHolder.mobile.setText(entity.mobile);

        GlideApp.with(context).load(entity.getAvatar())
                .error(R.drawable.head)
                .placeholder(R.drawable.head)
                .into(contactViewHolder.avatar);
    }

    @Override
    public boolean shouldHideDivider(int position, RecyclerView parent) {
        int type = parent.getAdapter().getItemViewType(position);

        if (type == TYPE_TITLE) {
            if (position > 0) {

            }
            return true;
        }
        return false;
    }

    static class IndexViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_index)
        TextView indexTitle;

        public IndexViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
